{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试脚本\n",
    "\n",
    "随机从文件中读取三组数据，拟合成线性回归方程。然后使用余下数据代入方程进行计算，并计算误差\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.stats import linregress\n",
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取上传的CSV文件\n",
    "df = pd.read_csv('../data/data_能巨_v2.csv')\n",
    "\n",
    "# 随机挑选三组数据\n",
    "sampled_data = df.sample(3)\n",
    "\n",
    "# 提取这三组数据的 pulse_count 和 gas_flow\n",
    "x_sample = sampled_data['pulse_count'].values.reshape(-1, 1)\n",
    "y_sample = sampled_data['gas_flow'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用线性回归模型拟合这三组数据\n",
    "slope, intercept, r_value, p_value, std_err = linregress(x_sample.flatten(), y_sample)\n",
    "\n",
    "print(f\"y = {slope:.6f}x + {intercept:.6f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用模型对整个数据集进行预测\n",
    "y_pred = slope * df['pulse_count'] + intercept\n",
    "df['y_pred'] = y_pred\n",
    "\n",
    "# 计算每个预测值与实际值之间的相对误差\n",
    "relative_errors = (y_pred - df['gas_flow']) / df['gas_flow']\n",
    "df['relative_errors'] = pd.DataFrame(relative_errors, columns=['Relative_Error'])\n",
    "# 设置数据格式为百分比\n",
    "df['Relative_Error_Percentage'] = df['relative_errors'].apply(lambda x: \"{:.2%}\".format(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存成csv文件\n",
    "\n",
    "df.to_csv(\"../data/data_能巨_v2_result.csv\",index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_values=[1311,1264,1261,1172,1066,644,399,481]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 线性回归方程的参数\n",
    "slope = 0.012205\n",
    "intercept = 0.000020\n",
    "\n",
    "# 使用线性回归方程计算 y 值\n",
    "y_values = [slope * x + intercept for x in x_values]\n",
    "y_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.686171\n"
     ]
    }
   ],
   "source": [
    "print(0.012952 * 621 + -0.357021)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
